//
//  NSDate+TYZAdd.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/5/30.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "NSDate+TYZAdd.h"

@implementation NSDate (TYZAdd)

/// 得到当前时间的前面日期
/// @param type 1 天；2 月
/// @param num 数量
- (NSDate *)getPreviousDate:(NSInteger)type andNum:(NSInteger)num {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    if (type == 1) {
        [components setDay:-num];
    } else if (type == 2) {
        [components setMonth:-num];
    }
    NSDate *date = [calendar dateByAddingComponents:components toDate:self options:0];
    return date;
}

/**
 *  将时间转化为相应格式的时间戳
 *
 *  @param dateString 时间
 *  @param format     format(e.g:@"yyyy-MM-dd HH:mm:ss")
 *
 *  @return stmap
 */
+ (NSTimeInterval)dateWithTimeStamp:(NSString *)dateString format:(NSString *)format
{
    return [self dateWithTimeStamp:dateString format:format timeZone:nil locale:nil];
}

/**
 *  返回时间戳
 *
 *  @param dateString The string to parse.
 *  @param format     he string's date format.
 *  @param timeZone   The time zone, can be nil.
 *  @param locale     The locale, can be nil.
 *
 *  @return 时间戳
 */
+ (NSTimeInterval)dateWithTimeStamp:(NSString *)dateString format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    if (timeZone)
    {
        [formatter setTimeZone:timeZone];
    }
    if (locale)
    {
        [formatter setLocale:locale];
    }
    NSDate *newDate = [formatter dateFromString:dateString];
    return [newDate timeIntervalSince1970];
}

/**
 *  返回时间戳
 *
 *  @param format he string's date format.(e.g:@"yyyy-MM-dd HH:mm:ss")
 *
 *  @return 时间戳
 */
- (NSTimeInterval)stringWithTimeStamp:(NSString *)format
{
    return [self stringWithTimeStamp:format timeZone:nil locale:nil];
}

/**
 *  时间戳
 *
 *  @param format   he string's date format.
 *  @param timeZone The time zone, can be nil.
 *  @param locale   The locale, can be nil.
 *
 *  @return 时间戳
 */
- (NSTimeInterval)stringWithTimeStamp:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    if (timeZone)
    {
        [formatter setTimeZone:timeZone];
    }
    if (locale)
    {
        [formatter setLocale:locale];
    }
    
    return [[formatter dateFromString:[formatter stringFromDate:self]] timeIntervalSince1970];
    
    //    return [self timeIntervalSince1970];
}

/**
 *  得到时间字符串
 *
 *  @param interval 时间戳
 *  @param format   时间格式(e.g:@"yyyy-MM-dd HH:mm:ss")
 *
 *  @return return value description
 */
+ (NSString *)timeStampWithString:(NSTimeInterval)interval format:(NSString *)format
{
    return [self timeStampWithString:interval format:format timeZone:nil locale:nil];
}

/**
 *  得到时间字符串
 *
 *  @param interval 时间戳
 *  @param format   format
 *  @param timeZone t
 *  @param locale e
 *
 *  @return 返回时间时间字符串
 */
+ (NSString *)timeStampWithString:(NSTimeInterval)interval format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    if (timeZone)
    {
        [formatter setTimeZone:timeZone];
    }
    if (locale)
    {
        [formatter setLocale:locale];
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    return [formatter stringFromDate:date];
}


+ (NSDate *)timeStampWithDate:(NSTimeInterval)interval format:(NSString *)format
{
    return [self timeStampWithDate:interval format:format timeZone:nil locale:nil];
}

+ (NSDate *)timeStampWithDate:(NSTimeInterval)interval format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    if (timeZone)
    {
        [formatter setTimeZone:timeZone];
    }
    if (locale)
    {
        [formatter setLocale:locale];
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    return date;
}

/// 两个日期想减，得到年数
/// @param startDate 开始时间
/// @param endDate 结束时间
/// @param format 格式
+ (NSInteger)yearsWithDateStringDiffer:(NSString *)startDate endDate:(NSString *)endDate format:(NSString *)format
{
    NSTimeInterval interval = [self _tyz_timervalWithDateStringDiffer:startDate endDate:endDate format:format];
    return (interval / (12 * 31 * 24 * 60 * 60));
}

/**
 *  两个日期想减，得到天数
 *
 *  @param startDate 开始时间
 *  @param endDate   结束时间
 *  @param format    格式
 *
 *  @return 天数
 */
+ (NSInteger)daysWithDateStringDiffer:(NSString *)startDate endDate:(NSString *)endDate format:(NSString *)format
{
    NSTimeInterval interval = [self _tyz_timervalWithDateStringDiffer:startDate endDate:endDate format:format];
    return (interval / (24 * 60 * 60));
}

/**
 *  两个时间相减，得到小时
 *
 *  @param startDate 开始时间
 *  @param endDate 结束时间
 *  @param format 格式
 *
 *  @return 返回小时
 */
+ (NSInteger)hoursWithDateStringDiffer:(NSString *)startDate endDate:(NSString *)endDate format:(NSString *)format
{
    NSTimeInterval interval = [self _tyz_timervalWithDateStringDiffer:startDate endDate:endDate format:format];
    return (interval / (60 * 60));
}

/// 分
+ (NSInteger)minutesWithDateStringDiffer:(NSString *)startDate endDate:(NSString *)endDate format:(NSString *)format
{
    NSTimeInterval interval = [self _tyz_timervalWithDateStringDiffer:startDate endDate:endDate format:format];
    return (interval / 60);
}

/// 秒
+ (NSInteger)secondsWithDateStringDiffer:(NSString *)startDate endDate:(NSString *)endDate format:(NSString *)format
{
    NSTimeInterval interval = [self _tyz_timervalWithDateStringDiffer:startDate endDate:endDate format:format];
    return interval;
}

+ (NSTimeInterval)_tyz_timervalWithDateStringDiffer:(NSString *)startDate endDate:(NSString *)endDate format:(NSString *)format
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSDate *start = [formatter dateFromString:startDate];
    NSDate *end = [formatter dateFromString:endDate];
    NSTimeInterval interval = [end timeIntervalSince1970] - [start timeIntervalSince1970];
    return interval;
}

/**
 *  将一种格式的字符串日期转化为另一种日期的字符串
 *
 *  @param strDate   时间
 *  @param inFormat  输入日期格式
 *  @param outFormat 输出日期格式
 *
 *  @return 时间
 */
+ (NSString *)stringWithDateInOut:(NSString *)strDate inFormat:(NSString *)inFormat outFormat:(NSString *)outFormat
{
    NSDate *date = [NSDate dateWithString:strDate format:inFormat];
    return [date stringWithFormat:outFormat];
}

/// 获取当前的时间戳(毫秒)
+ (NSString *)stringWithCurrentTimeStamp
{
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval stamp = [date timeIntervalSince1970] * 1000; // *1000是精确到毫秒，不乘就是精切到秒
//    debugLog(@"%f", stamp);
    // time(NULL)
    return [NSString stringWithFormat:@"%.0f", stamp];
}

/**
 比较两个时间的大小
 
 @param otherDate 比较的时间
 @param format 格式
 @return 0 时间相等；1 过期；-1 没有过期
 */
- (int)compareTime:(NSDate *)otherDate format:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    
    NSString *oneDayStr = [dateFormatter stringFromDate:self];
    NSString *anotherDayStr = [dateFormatter stringFromDate:otherDate];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
//    NSLog(@"oneDay : %@, anotherDay : %@", self, otherDate);
    if (result == NSOrderedDescending)
    {
        //在指定时间前面 过了指定时间 过期
//        NSLog(@"oneDay  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending)
    {
        //没过指定时间 没过期
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //刚好时间一样.
    //NSLog(@"Both dates are the same");
    return 0;
}

//- (NSDate *)getCurrentTime
//{
//    //    NSString *dateTime = [[NSDate date] stringWithFormat:@"yyyy-MM-dd "];
//    //    return [NSDate dateWithString:dateTime format:@"yyyy-MM-dd "];
//    //2017-04-24 08:57:29
//    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
//    [formatter setDateFormat:@"yyyy-MM-dd "];
//    NSString *dateTime=[formatter stringFromDate:[NSDate date]];
//    NSDate *date = [formatter dateFromString:dateTime];
//    return date;
//}

- (NSDate *)currentDateFromat:(NSString *)format
{
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:format];
    NSString *dateTime=[formatter stringFromDate:self];
    NSDate *date = [formatter dateFromString:dateTime];
    return date;
}

//比较时间是否相同
//- (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
//{
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
//    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
//    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
//    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
//    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
//    NSComparisonResult result = [dateA compare:dateB];
//    NSLog(@"oneDay : %@, anotherDay : %@", oneDay, anotherDay);
//    if (result == NSOrderedDescending) {
//        //在指定时间前面 过了指定时间 过期
//        NSLog(@"oneDay  is in the future");
//        return 1;
//    }
//    else if (result == NSOrderedAscending){
//        //没过指定时间 没过期
//        //NSLog(@"Date1 is in the past");
//        return -1;
//    }
//    //刚好时间一样.
//    //NSLog(@"Both dates are the same");
//    return 0;
//
//}

/**
 更加日期得到星期
 
 @param date 日期
 @param format 格式
 @return return value description
 */
+ (NSString *)weekWithDate:(NSString *)date format:(NSString *)format isShort:(BOOL)isShort
{
    NSDate *curDate = [self dateWithString:date format:format];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *coms = [calendar components:NSCalendarUnitWeekday fromDate:curDate];
    NSString *week = @"";
    switch (coms.weekday)
    {
        case 1:
            week = (isShort?@"日":@"星期日");
            break;
        case 2:
            week = (isShort?@"一":@"星期一");
            break;
        case 3:
            week = (isShort?@"二":@"星期二");
            break;
        case 4:
            week = (isShort?@"三":@"星期三");
            break;
        case 5:
            week = (isShort?@"四":@"星期四");
            break;
        case 6:
            week = (isShort?@"五":@"星期五");
            break;
        case 7:
            week = (isShort?@"六":@"星期六");
            break;
        default:
            break;
    }
    return week;
}

//NSDate *date = [NSDate date];
//NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
//// dayCountOfThisMonth就是date月份的天数
//NSInteger dayCountOfThisMonth = daysInLastMonth.length;

/// 获取当前月份的天数
- (NSInteger)currentMonthDays
{
//    NSDate *date = [NSDate date];
    NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self];
    // dayCountOfThisMonth就是date月份的天数
    NSInteger dayCountOfThisMonth = daysInLastMonth.length;
    return dayCountOfThisMonth;;
}

/// 获取某月的第一天
/// @param dateStr dateStr description
/// @param format format description
+ (NSString *)getMonthFirstDay:(NSString *)dateStr format:(NSString *)format
{
    if (!dateStr) {
        return nil;
    }
    
    NSDate *newDate = [NSDate dateWithString:dateStr format:format];
    
    double interval = 0;

    NSDate * firstDate = nil;

    NSCalendar * calendar = [NSCalendar currentCalendar];

    BOOL bl = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&firstDate interval:&interval forDate:newDate];

    if(bl) {

        NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc]init];

        [myDateFormatter setDateFormat:format];

        NSString * firstString = [myDateFormatter stringFromDate: firstDate];

        return firstString;
    }

    return nil;
}

/// 获取最后一天
/// @param dateStr dateStr description
/// @param format format description
+ (NSString *)getMonthLastDay:(NSString *)dateStr format:(NSString *)format
{
    if (!dateStr) {
        return nil;
    }
    
    NSDate *newDate = [NSDate dateWithString:dateStr format:format];
    double interval = 0;
    NSDate * firstDate = nil;

    NSDate * lastDate = nil;

    NSCalendar * calendar = [NSCalendar currentCalendar];

    BOOL bl = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&firstDate interval:&interval forDate:newDate];

    if(bl) {

        lastDate = [firstDate dateByAddingTimeInterval:interval -1];

        NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc]init];

        [myDateFormatter setDateFormat:format];

        NSString * lastString = [myDateFormatter stringFromDate:lastDate];

        return lastString;
    }

    return nil;
}
@end
